package at.fhooe.obd.gui;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ChooseTrackActivity extends Activity implements OnItemClickListener
{
	String[] processedTracks = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_track);
		
		ListView lv = (ListView) findViewById(R.id.lv_chooseTrack);
		lv.setOnItemClickListener(this);

		processedTracks = ((String[])getIntent().getExtras().get("processedTracks"));
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this, android.R.layout.simple_list_item_1,
				processedTracks);

		lv.setAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_choose_track, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg, View lv, int position, long arg3)
	{
		if (lv instanceof TextView)
		{
			TextView tv = (TextView)lv;
			
			String text = tv.getText().toString();
			String trackId = text.substring(
					text.indexOf("Nr.") + 3, 
					text.indexOf("from")).trim();
			
			Log.i("", trackId);
			
			Intent intent = new Intent(this, DisplayTrackActivity.class);
			intent.putExtra(Keys.JSON_TRACK_ID, trackId);
			this.startActivity(intent);
		}
		
	}

}
